package com.sphd.restaurantsystem.model;


import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.info.FoodInfo;
import com.sphd.restaurantsystem.interfaces.VolleyJsonObjectCallBack;
import com.sphd.restaurantsystem.interfaces.VolleyStringCallBack;
import com.sphd.restaurantsystem.presenter.OrdersInfoPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
* Created by Hu on 2016/12/12
*/

public class OrdersInfoModelImpl implements OrdersInfoModel, VolleyJsonObjectCallBack {
    OrdersInfoPresenterImpl ordersInfoPresenter;
    @Override
    public void sendUrl(OrdersInfoPresenterImpl ordersInfoPresenter, String url) {
        this.ordersInfoPresenter=ordersInfoPresenter;
        volleyUtil.setJsonObjectRequest(url,this);
    }

    @Override
    public void recUrlMap(String s, Map<String, String> map, OrdersInfoPresenterImpl ordersInfoPresenter) {
        this.ordersInfoPresenter=ordersInfoPresenter;
        volleyUtil.setStringRequest(s, new VolleyStringCallBack() {
            @Override
            public void onResponse(String response) {
                int res=JsonUtil.getJsonObject(response).getIntValue("res");
                ordersInfoPresenter.recRes(res);
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                ordersInfoPresenter.sendError(error);
            }
        }, map);
    }

    @Override
    public void sendCancleUrl(String s, OrdersInfoPresenterImpl ordersInfoPresenter) {
        this.ordersInfoPresenter=ordersInfoPresenter;
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int status=response.getInt("status");
                    ordersInfoPresenter.recCanRes(status);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                ordersInfoPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            List<FoodInfo> foodInfos= JsonUtil.getListFromString(response.getString("res"),FoodInfo.class);
            ordersInfoPresenter.sendList(foodInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        ordersInfoPresenter.sendError(error);
    }
}